//
//  UIView+MyViewCategory.m
//  JHLNHV2
//
//  Created by macbook-air on 15/5/7.
//  Copyright (c) 2015年 macbook-air. All rights reserved.
//

#define kKNTopLayer @"KNTopLayer"
#define kKNBottomLayer @"KNBottomLayer"
#define kKNLeftLayer @"KNLeftLayer"
#define kKNRightLayer @"KNRightLayer"

#import "UIView+KNViewCategory.h"
#import <objc/runtime.h>

@implementation UIView (KNViewCategory)


@dynamic border;

static char KNColorKey;

static char KNWidthKey;


-(void)setBorderColor:(UIColor *)borderColor
{
    objc_setAssociatedObject(self, &KNColorKey, borderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIColor *)borderColor
{
    return objc_getAssociatedObject(self, &KNColorKey);
}

-(void)setBorderWidth:(NSNumber *)borderWidth
{
    objc_setAssociatedObject(self, &KNWidthKey, borderWidth, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSNumber *)borderWidth
{
    return objc_getAssociatedObject(self, &KNWidthKey);
}

-(void)setBorder:(KNViewBorder)border
{
    
    if (border & KNViewBorderRight)
    {
        [self KNAddRightBorderWithColor:self.borderColor andWidth:[self.borderWidth doubleValue]];
    }
    if (border & KNViewBorderBottom)
    {
        [self KNAddBottomBorderWithColor:self.borderColor andWidth:[self.borderWidth doubleValue]];
    }
    if (border & KNViewBorderTop)
    {
        [self KNAddTopBorderWithColor:self.borderColor andWidth:[self.borderWidth doubleValue]];
    }
    if (border & KNViewBorderLeft)
    {
        [self KNAddLeftBorderWithColor:self.borderColor andWidth:[self.borderWidth doubleValue]];
    }
    if (border & KNViewBorderNone) {
        [self KNRemoveAllSubLayers];
    }
}

- (void)KNRemoveAllSubLayers {
    NSMutableArray *removeLayers = @[].mutableCopy;
    [self.layer.sublayers enumerateObjectsUsingBlock:^(CALayer *layer, NSUInteger idx, BOOL *stop) {
        if ([[layer name] isEqualToString:kKNTopLayer] ||
            [[layer name] isEqualToString:kKNBottomLayer] ||
            [[layer name] isEqualToString:kKNLeftLayer] ||
            [[layer name] isEqualToString:kKNRightLayer] )
        {
            [removeLayers addObject:layer];
        }
    }];
    
    [removeLayers enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperlayer];
    }];
}

- (void)KNAddTopBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {
    __block BOOL find = NO;
    [self.layer.sublayers enumerateObjectsUsingBlock:^(CALayer *layer, NSUInteger idx, BOOL *stop) {
        if ([[layer name] isEqualToString:kKNTopLayer]) {
            layer.backgroundColor = color.CGColor;
            layer.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth);
            *stop = YES;
            find = YES;
            return ;
        }
        
    }];
    
    if (find) {
        return;
    }
    
    CALayer *border = [CALayer layer];
    border.name = kKNTopLayer;
    border.backgroundColor = color.CGColor;
    
    border.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth);
    [self.layer addSublayer:border];
}

- (void)KNAddBottomBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {
    __block BOOL find = NO;
    [self.layer.sublayers enumerateObjectsUsingBlock:^(CALayer *layer, NSUInteger idx, BOOL *stop) {
        if ([[layer name] isEqualToString:kKNBottomLayer]) {
            layer.backgroundColor = color.CGColor;
            layer.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth);
            *stop = YES;
            find = YES;
            return ;
        }
        
    }];
    
    if (find) {
        return;
    }
    CALayer *border = [CALayer layer];
    border.name = kKNBottomLayer;
    border.backgroundColor = color.CGColor;
    
    border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth);
    [self.layer addSublayer:border];
}

- (void)KNAddLeftBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {
    __block BOOL find = NO;
    [self.layer.sublayers enumerateObjectsUsingBlock:^(CALayer *layer, NSUInteger idx, BOOL *stop) {
        if ([[layer name] isEqualToString:kKNLeftLayer]) {
            layer.backgroundColor = color.CGColor;
            layer.frame = CGRectMake(0, 0, borderWidth, self.frame.size.height);
            *stop = YES;
            find = YES;
            return ;
        }
        
    }];
    
    if (find) {
        return;
    }
    
    CALayer *border = [CALayer layer];
    border.name = kKNLeftLayer;
    border.backgroundColor = color.CGColor;
    
    border.frame = CGRectMake(0, 0, borderWidth, self.frame.size.height);
    [self.layer addSublayer:border];
}

- (void)KNAddRightBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {
    __block BOOL find = NO;
    [self.layer.sublayers enumerateObjectsUsingBlock:^(CALayer *layer, NSUInteger idx, BOOL *stop) {
        if ([[layer name] isEqualToString:kKNRightLayer]) {
            layer.backgroundColor = color.CGColor;
            layer.frame = CGRectMake(self.frame.size.width - borderWidth, 0, borderWidth, self.frame.size.height);
            *stop = YES;
            find = YES;
            return ;
        }
        
    }];
    
    if (find) {
        return;
    }
    
    CALayer *border = [CALayer layer];
    border.name = kKNRightLayer;
    border.backgroundColor = color.CGColor;
    
    border.frame = CGRectMake(self.frame.size.width - borderWidth, 0, borderWidth, self.frame.size.height);
    [self.layer addSublayer:border];
}

- (void)KNSetBorderRaidus {
    [self KNSetBorderRaidus: self.frame.size.width / 2.0f borderwidth: 0.0f bordercolor: nil];
}

- (void)KNSetBorderRaidus:(CGFloat)radius borderwidth:(CGFloat)borderwidth bordercolor:(UIColor *)bordercolor {
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = radius;
    self.layer.borderColor = [bordercolor CGColor];
    self.layer.borderWidth = borderwidth;
}

- (void)KNSetBorderRaidus:(CGFloat)radius borderwidth:(CGFloat)borderwidth bordercolor:(UIColor *)bordercolor shadowColor:(UIColor *)shadowColor shadowOpacity:(CGFloat)opacity offSet:(CGSize)offset shaowRadius:(CGFloat)shadowRadius {
    [self KNSetBorderRaidus:radius borderwidth:borderwidth bordercolor:bordercolor];
    self.layer.masksToBounds = NO;
    self.layer.shadowColor=shadowColor.CGColor;
    self.layer.shadowOffset=offset;
    self.layer.shadowOpacity=opacity;
    self.layer.shadowRadius=shadowRadius;
}

- (CALayer *)KNDrawALine:(CGRect)frame color:(CGColorRef)color {
    CALayer *line = [CALayer new];
    line.backgroundColor = color;
    line.frame = frame;
    [self.layer addSublayer: line];
    return line;
}

@end
